package br.ufrj.management.pages.virtual;

import java.util.ArrayList;


public class SimThread implements Runnable {
	
	private int id;
	public ArrayList<Integer> working_set_table; //the working set of the process
	private int process_size_in_pages; //the size of the process in number of pages
	private Integer newPage;
	
	public SimThread(int id, int process_ws_max, int process_size_in_pages){
		
		this.id = id;
		this.process_size_in_pages = process_size_in_pages;
		working_set_table = new ArrayList<Integer>(process_ws_max);
		
		
		for(int i = 0; i < process_ws_max; i++){
			working_set_table.add(new Integer(-1));
		}
		
		(new Thread(this)).start();
	}
	
	public int getId(){
		return this.id;
	}
	
	@Override
	public void run() {
		
		for(;;){
			try {
				Thread.sleep(2000);
				if (MemoryManager.running_threads.get(id) != null)
				{
					newPage = new Integer ( (int) (Math.random()*(process_size_in_pages-1)) );				
					MemoryManager.requestPage(newPage, id);
				}
				else
				{
					break;
				}
				//System.out.println("Thread ID: " + this.id + " Message: " + this.working_set_table.toString());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
}
